/*
 * @Author: 余其方 448919239@qq.com
 * @Date: 2022-08-02 20:55:35
 * @LastEditors: 余其方 448919239@qq.com
 * @LastEditTime: 2022-09-01 16:16:58
 * @FilePath: /redux-demo/src/sagas.js
 * @Description:
 */
import { takeEvery, put } from "redux-saga/effects";
import { GET_INIT_LIST } from "./store/actionTypes";
import { initListAction } from "./store/actionCreators";
import axios from "axios";

// generator（发电机，生成器） 函数
function* initListFn() {
  try {
    const res = yield axios.get("/list.json");
    const action = initListAction(res.data);
    yield put(action);
  } catch (e) {
    console.log("网络请求失败", e);
  }
}

// 原本redux只能在reducer中接收action做执行
// 使用了 redux-sage 之后，就可以在 sagas.js 文件的函数中通过 yield takeEvery() 来捕获到每一次 dispatch 派发出来 action 了
function* mySaga() {
  yield takeEvery(GET_INIT_LIST, initListFn);
}

export default mySaga;
